package com.example.myapplication;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.Nullable;


/**
 * @author:weng
 * @data:2022/11/7
 * @description:
 */

public class WebViewActivity extends Activity {
    private static final String URI = "URI";
   private WebView webView;
    public static void start(Context context, String content) {
        Intent intent = new Intent(context, WebViewActivity.class);
        intent.putExtra(URI, content);
        context.startActivity(intent);
    }

    public static void start(Context context) {
        context.startActivity(new Intent(context, WebViewActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        webView = findViewById(R.id.webView);
        setTitle("WEB");
        initWebView();
    }

    @SuppressLint("JavascriptInterface")
    private void initWebView() {
        if ( webView!= null) {
            WebSettings webSettings = webView.getSettings();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                webSettings.setSafeBrowsingEnabled(false);
            }
            webSettings.setJavaScriptEnabled(true);
            webSettings.setBlockNetworkImage(false);
            webSettings.setLoadsImagesAutomatically(true);
            webSettings.setUseWideViewPort(true);
            webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
            webSettings.setAllowFileAccess(true);
            webSettings.setLoadWithOverviewMode(true);
            webSettings.setSaveFormData(false);
            webSettings.setDomStorageEnabled(true);
            webSettings.setBuiltInZoomControls(false);
            webSettings.setSupportZoom(false);
            webSettings.setPluginState(WebSettings.PluginState.ON);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                webSettings.setTextSize(WebSettings.TextSize.NORMAL);
                try {
                    webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
            }

            String content = getIntent().getStringExtra(URI);
            Log.i("tag", "content =" + content);

            webView.loadUrl("https://h5.hengqifeng.com/myweek/MyweekPrivacyAgreement/index.html");

            webView.setNetworkAvailable(true);
            webView.setInitialScale(1);
            webView.requestFocus();

            webView.addJavascriptInterface(this, "Android");

            webView.setWebChromeClient(new WebChromeClient() {

            });


            webView.setWebViewClient(new WebViewClient() {

            });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (webView != null) {
            webView.destroy();
        }
    }

}
